package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.CustomerTraceHistory;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.CustomerTraceHistoryQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerTraceHistoryService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customerTraceHistory")
public class CustomerTraceHistoryController {
    
    @Autowired
    private ICustomerTraceHistoryService customerTraceHistoryService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"跟进历史列表权限","dept:list"},logical = Logical.OR)
    public String list(@ModelAttribute("qo") CustomerTraceHistoryQueryObject qo, Model model) {

        //权限控制
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        //
        if(!(employee.isAdmin()||SecurityUtils.getSubject().hasRole("Market_Manager"))){
            //不是超管 也不是市场经理  设置当前用户的id 作为条件查询页面
            qo.setSellerId(employee.getId());
        }
        PageInfo<CustomerTraceHistory> pageInfo = customerTraceHistoryService.selectByQo(qo);
        model.addAttribute("pageInfo", pageInfo);

        //查询跟进方式
        List<SystemDictionaryItem> traceType = systemDictionaryItemService.listByDictionarySn("communicationMethod");
        model.addAttribute("traceType",traceType);

        return "customertracehistory/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"跟进历史编辑或新增权限","dept:saveOrUpdate"},logical = Logical.OR)
    public JsonResult saveOrUpdate(CustomerTraceHistory customerTraceHistory, Model model) {
        JsonResult result = new JsonResult();
        try {
            System.out.println("customerTraceHistory.getTraceTime() = " + customerTraceHistory.getTraceTime());
            customerTraceHistory.setType(Customer.STATUS_POTENTIAL);
            customerTraceHistoryService.saveOrUpdate(customerTraceHistory);

        } catch (Exception e) {
            e.printStackTrace();
            result.mark("保存失败");
        }
        return result;
    }




}
